﻿/*----------------------------------------------------------------

//----------------------------------------------------------------*/
using System;
using System.Data.Common;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;

using FamilyTree.DAL;
using FamilyTree.Entity;
using System.Data;
using EnumsNET;
using SqlSugar;
using FamilyTree.DLL;

namespace FamilyTree.BLL
{
    /// <summary>
    /// 提供处理各种管理成员  [TB_Member]表的业务逻辑的方法
    /// </summary>
	/// <author>jinchuanliang 2018-07-14</author>
    public sealed class BMember:WebDataAccess
    {
        /// <summary>
        /// 私有的构造函数。
        /// </summary>
        private BMember()
        {
        }

        /// <summary>
        /// 公共的入口。
        /// </summary>
        /// <returns></returns>
        public static BMember BLL
        {
            get { return Bll; }
        }

        /// <summary>
        /// 业务逻辑层对象
        /// </summary>
        private readonly static BMember Bll = new BMember();

        /// <summary>
        /// 添加成员数据
        /// </summary>
        /// <param name="model">数据实体</param> 
        /// 创建人：jinchuanliang 时间：2016-09-22
        /// <returns></returns>
        public int Insert(MemberEntity model)
        {
            int id = SugarClient.Insertable(model).ExecuteReturnIdentity();
            return id;
        }


        /// <summary>
        /// 修改tb_member数据
        /// </summary>
        /// <param name="model">数据实体</param> 
        /// 创建人：jinchuanliang 时间：2018-07-14
        /// <returns></returns>
        public bool Update(MemberEntity model)
        {
           int i = SugarClient.Updateable(model).ExecuteCommand();

            return i==1;
        }

        /// <summary>
        /// 删除tb_member数据
        /// </summary>
        /// <param name="memberId">成员ID </param> 
        /// 创建人：jinchuanliang 时间：2018-07-14
        /// <returns></returns>
        public bool SignDelete(int memberId)
        {
            //return Dal.SignDelete(memberId);

            return false;

        }


        /// <summary>
        ///  获取ModelList
        /// </summary> 
        /// <param name="exid"></param>
        /// <returns></returns>
        public MemberEntity GetModel(int memberId)
        {
            if (memberId == 0)
            {
                return new MemberEntity();
            }
            //return Dal.GetModel(memberId);
           return  SugarClient.Queryable<MemberEntity>().Where(w => w.IsDelete == 0 && w.MemberId == memberId).First();
             
        }


        /// <summary>
        ///  获取ModelList
        /// </summary> 
        /// <param name="familyInfoId"></param>
        /// <returns></returns>
        public List<MemberEntity> GetModels(int familyInfoId)
        {
            //return Dal.GetModels(familyInfoId);

            return null;
        }

        /// <summary>
        ///  获取ModelList
        /// </summary> 
        /// <param name="familyInfoId"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<MemberEntity> GetModels(int familyInfoId, int parentId)
        {
            //return Dal.GetModels(familyInfoId,parentId);

            return null;
        }

        /// <summary>
        ///  获取ModelList
        /// </summary> 
        /// <param name="familyInfoId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<MemberEntity> GetModels(int familyInfoId, int pageIndex, int pageSize)
        {
            //return Dal.GetModels(familyInfoId, pageIndex, pageSize);

            return null;
        }

        /// <summary>
        /// //安全过虑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static MemberEntity GetSafetyModel(MemberEntity model)
        {
            //MemberEntity mod = new MemberEntity();
            //mod.MemberId = model.MemberId;
            //mod.MemberName = XSSTool.Filter(model.MemberName);
            //mod.Sex = XSSTool.Filter(model.Sex);
            //mod.CardNumber = XSSTool.Filter(model.CardNumber);
            //mod.Phone = XSSTool.Filter(model.Phone);
            //mod.Birthday = model.Birthday;
            //mod.FamilyInfoId = model.FamilyInfoId;
            //mod.Ranking = model.Ranking;
            //mod.PicUrl = XSSTool.Filter(model.PicUrl);
            //mod.Profile = XSSTool.Filter(model.Profile);
            //mod.DieTime = model.DieTime;
            //mod.IsDelete = model.IsDelete; 
            //mod.ParentId = model.ParentId;
            //mod.Remarks = XSSTool.Filter(model.Remarks);

            //return mod;

            return null;
        }

        DataTable MemberDt = new DataTable();
        List<MemberEntity> Members;
        public List<MemberEntity> GetMemberInfos(int fid)
        {
            //string memberFile = "Member.xlsx";

            //MemberDt = NPOIHelper.ImportExceltoDt(memberFile);
            //FillMenus(Menus, "0");
            Members = SugarClient.Queryable<MemberEntity>().Where(w=>w.IsDelete==0 && w.FamilyInfoId==fid).ToList();

            List<MemberEntity> Menus = new List<MemberEntity>();
            FillMenus2(Menus, 0);

            return Menus;
        }


        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentId"></param>
        private void FillMenus(List<MemberEntity> menus, string parentId)
        {
            if (MemberDt == null) return;
            var sub = MemberDt.Select($"ParentId='{parentId}'");

            if (sub.Count() > 0)
            {
                foreach (var item in sub)
                {
                    MemberEntity mm = new MemberEntity()
                    {
                        MemberId = int.Parse(item["MID"].ToString()),

                        MemberName = item["名称"].ToString(),
                        ParentId = int.Parse(item["ParentId"].ToString()),
                        Remarks= $"{item["RName"]}",
                    };
                    menus.Add(mm);

                    FillMenus(mm.children = new List<MemberEntity>(), item["MID"].ToString());
                }
            }
        }
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentId"></param>
        private void FillMenus2(List<MemberEntity> menus, int parentId)
        {
            if (Members == null) return;
            try
            {
                var sub = Members.Where(w => w.ParentId == parentId).ToList();

                if (sub.Count() > 0)
                {
                    foreach (var item in sub)
                    {
                        menus.Add(item);

                        FillMenus2(item.children = new List<MemberEntity>(), item.MemberId);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }




    }
}

